define(["require","business/personalcenter/service/shoppingHttp",'config/appregister',"business/vedio/services/httpServices","business/services/constant","business/services/moreHttpServices",
    "jquery","business/vedio/dir/http", "business/services/commentComp","business/workSkill/directive/startLevel",
    "business/home/directives/share","business/Companyinfor/service/chinese","business/vedio/dir/scroll"],function(require,http,app,httpsevices,constant,moreHttpServices) {
    app.controller('detailsCtrl', function ($anchorScroll,$scope,$window, $http,$location, $state,$stateParams,auth,$rootScope,$http) {

	  	
		$scope.showPlay=function(){
			 var req ={
                    type:"POST",
	                    uri:"add",
	                    data:{
	                    	courseId:$stateParams.courseId
	                    }
	                };
	                httpsevices.getenroll(req).success(function(response){
	                    $scope.$apply(function(){
	                        if(response.code == 0 | response.code==304 ){
                              
								window.open('http://www.gongchengjie.cn/#/eng/videoPlay?couseIdPlayId='+$stateParams.courseId,'_blank')
							}
	                    })
	                });
	      
		}
		$scope.goTo = function(item){
		 	var videoId = $(".vediotime").attr("id");
        	$state.go("eng.videoPlay",{couseIdPlayId:videoId})
        }
        $scope.switchVideo=function(item){
            $state.go("eng.vedioDetails",{courseId:item.courseId})
        }
		  $scope.goTo2 = function(item){
		  	 auth.isLogin().then(function(){
                               
             })
		  	window.open('http://www.gongchengjie.cn/#/eng/videoPlay?couseIdPlayId='+$stateParams.courseId+'+&videoPlayId='+item.videoId)
		  	
        	/*$state.go("eng.videoPlay",{videoPlayId:item.videoId,couseIdPlayId:$stateParams.courseId})*/
        	
        }
		  $scope.goto3=function(item){

		  }
		  $scope.go=function(){
		  	$state.go("eng.vedio")
		  }
		  /*视频列表回头显示*/
		  $scope.golistOn=function(id){
		  	sessionStorage.topId = id
		  	sessionStorage.removeItem("twoId")
		  	sessionStorage.removeItem("thrId")
		  	$state.transitionTo("eng.vedio", {}, {
                reload: true, inherit: false, notify: true
            });
		  	/*$state.go("eng.vedio")*/
		  }
		  $scope.golistTw=function(id1,id){
              sessionStorage.removeItem("thrId")
              sessionStorage.topId = id1
              sessionStorage.twoId = id
              $state.transitionTo("eng.vedio", {}, {
                reload: true, inherit: false, notify: true
            });
		  }
		  $scope.golistTh=function(id1,id2,id3){
		  	sessionStorage.topId = id1
		  	sessionStorage.twoId = id2
		  	sessionStorage.thrId = id3
		  	$state.transitionTo("eng.vedio", {}, {
                reload: true, inherit: false, notify: true
            });
		  }
        /*星级*/
        $scope.max = 5;//最大星级
        $scope.ratingVal = 1;///HTML中也可传递默认星级，必需设置，下面同样名字覆盖，否则不显示；
        $scope.readonly = true;//false可编辑，true不可编辑
        $scope.onHover = function(val){
            $scope.hoverVal = val;
        };
        $scope.onLeave = function(){
            $scope.hoverVal = null;
        }
        $scope.onChange = function(val){
            $scope.ratingVal = val;
        };
        /*end*/
        $scope.isDetailPage = true;
        $scope.isMainpage = false;
         	if($stateParams.hidePlay=="1"){
				$scope.jodes=1;
			}else{
				$scope.jodes=2;
			}
        function getCompangyInfo(id){
            $http.get('http://116.62.10.111:8080/elink_user/api/company/getBaseInfoByCompany?companyId='+id).then(function(data){
                $scope.compangyInfo=data.data.data;
            })
        }
        if ($stateParams.courseId) {
			
            var couseId = $stateParams.courseId;
            /*头部资料*/
           
            var req ={
                type:"GET",
                uri:"get?courseId=" + couseId,
                data:{}
            };
            httpsevices.getcourse(req).success(function(response){
                $scope.$apply(function(){
                
                	 $scope.share = {
                	     url:"www.qq.com",
					 	summary:response.data.course.title,
					 	title:response.data.course.price
					 }
                	$scope.TopLink = response.data;
                    $scope.course = response.data.course;
                    $scope.courseTit = response.data.course.title;
                    $scope.size = response.data.course.size;
                    $scope.price = response.data.course.price;
                    $scope.cousId = response.data.course.courseId;
                    $scope.studys=response.data.course.downloadCount;
                    $scope.ratingVal=response.data.course.avgCount;//星星
                    $scope.peopleNum=response.data.course.evalCount;//星星

                    $scope.labid = response.data.course.labelList;
                    $scope.teacher = response.data.course.teacher;
                    $scope.teachId = response.data.course.teacher.userId;
                    $scope.teacherCoun=response.data.teacherCount;
                    $scope.courseImg = response.data.course.previewPath;
                    $scope.introduction = response.data.course.introduction;
                    $scope.teacherCOunt = response.data.teacherCount;
                    $scope.coursePrice  = parseInt(JSON.stringify(response.data.course.price));
                    var price = parseInt(JSON.stringify(response.data.course.price));
                    var couseId = response.data.course.courseId;
                    getCompangyInfo($scope.course.createUserId);
                    if(response.data.course.courseCollectionFlag == true){
                        $(".collection").text("已收藏")
                        $(".collection").css("background","url(them/img/collectioned.png) 0px 2px no-repeat")
                        $(".collection").addClass("commonColor")
                    }
                   
                    if(response.data.enrollCount == 0){
                        if(price == 0){
                            $(".zero").text('免费');
                        }else{
                            $(".zero").text(price+'￥');
                            $("#nowbtn").attr("href","javascript:;").text("立即报名");
                        }
                       

                        $("#nowbtn").attr("href","javascript:;").text("立即报名");
                        $("#nowbtn").click(function(){
                           auth.isLogin().then(function(){
                                /*updateCollection();*/
                            })
                            $('.Popup').show();
                            $('.cover').show();
                            var height = $(document.body).height()+"px";
                            $(".cover").animate({"height":height},1000)
                            $(".Popup").css("z-index","9999");
                            var req ={
                                type:"GET",
                                uri:"getCourseEnroll",
                                data:{
                                    courseId:couseId
                                }
                            };
                            httpsevices.getenroll(req).success(function(response){
                                $scope.$apply(function(){
                                    $scope.signData = response.data;
                                    $scope.signTit = response.data.title;
                                    $scope.signPrice= response.data.price;
                                    $scope.signCouseId = response.data.courseId;

                                })
                            });
                        });

                    }else{
                        $("#nowbtn").click(function(){
                            auth.isLogin().then(function(){
                               /* updateCollection();*/
                            })
                           window.open('http://www.gongchengjie.cn/#/eng/videoPlay?couseIdPlayId='+$stateParams.courseId,'_blank')
                        })
                    }
                })
            });
            $scope.signUp = function(){
                if($scope.coursePrice == 0){
                    var req ={
                        type:"POST",
                        uri:"add",
                        data:{
                            courseId:couseId
                        }
                    };
                    httpsevices.getenroll(req).success(function(response){
                        $scope.$apply(function(){
                            if(response.code == 0 | response.code==304 ){
                                $("#nowbtn").text("立即学习");
                                $("#nowbtn").attr("id","nowbtn2")
                                $("#nowbtn2").click(function(){
                                    window.open('http://www.gongchengjie.cn/#/eng/videoPlay?couseIdPlayId='+$stateParams.courseId)
                                    $(".Popup ").hide();
                                    $(".cover").hide();
                                })
                                var txt = response.msg;
                                $(".PopupTop").text(txt);
                                $("#signUp").text("立即返回").attr("id","signUp2");
                                $("#signUp2").click(function(){
                                    $(".cover").hide();
                                    $(".Popup").hide();
                                });

                            }
                        })
                    });
                }else{

                    var sunmitList=[{
                        totalMoney:$scope.coursePrice,
                        businessId:$rootScope.userLoginInfo.data.userId
                    }];
                    var req = {
                        type:'POST',
                        uri:'submitOrder',
                        data:{
                            jsonPc:JSON.stringify(sunmitList),
                            courseIds:$stateParams.courseId
                        }
                    };
                    http.orderHttpService(req).success(function(response){
                        $scope.$apply(function(){
                            $state.go('payNowCtrl',{payId:response.data.payIds,courseId:response.data.courseIds,orderType:1});
                        })
                    }).error(function(response){

                    });
                }

            };
            /*目录*/
            function getCh(){
                var req ={
                    type:"GET",
                    uri:"getCourseCatalog",
                    data:{
                    	courseId:couseId
                    }
                };
                httpsevices.getchapter(req).success(function(response){
                    $scope.$apply(function(){
                        $scope.courseData = response.data;
                    })
                });
            }
            getCh();
            /*收藏功能*/
            $scope.collection = function(){
               auth.isLogin().then(function(){
                   
                })
                moreHttpServices.updateCollection($stateParams.courseId,8).success(function(response){
                    $scope.$apply(function(){
                        if($scope.colle > 0){
                            $(".collection").text("已收藏")
                            $(".collection").css("background","url('them/img/collectioned.png') 0px 2px no-repeat")
                            $(".collection").addClass("commonColor")
                            var req ={
                                type:"GET",
                                uri:"update",
                                data:{
                                    courseId:couseId
                                }
                            };
                            httpsevices.getcollect(req).success(function(response){
                                $scope.$apply(function(){
                                    if(response.code == 111){
                                        $(".collection").addClass("commonColor");
                                        $(".collection").text("已收藏")
                                        $(".collection").css("background","url(them/img/collectioned.png) 0px 2px no-repeat")

                                    }else if(response.code == 112){
                                        $(".collection").removeClass("commonColor");
                                        $(".collection").text("收藏")
                                        $(".collection").css("background","url(them/imgs/collection.png) 0px 2px no-repeat")
                                    }
                                })
                            });
                        }
                        else {
                            var req ={
                                type:"GET",
                                uri:"update",
                                data:{
                                    courseId:couseId
                                }
                            };
                            httpsevices.getcollect(req).success(function(response){
                                $scope.$apply(function(){
                                    if(response.code == 111){
                                        $(".collection").addClass("commonColor");
                                        $(".collection").css("background","url(them/img/collectioned.png) 0px 2px no-repeat")
                                        $(".collection").text("已收藏")

                                    }else if(response.code == 112){
                                        $(".collection").removeClass("commonColor");
                                        $(".collection").text("收藏")
                                        $(".collection").css("background","url(them/imgs/collection.png) 0px 2px no-repeat")

                                    }
                                })
                            });
                        }

                    })
                }).error(function(response){
                    console.log(response)
                })
            }

        }
        $scope.linkSearch=function(name){
            $rootScope.GLOBALKEYWORD =  name;
            sessionStorage.searchKeyWord = name;
            $state.transitionTo("search.course", {}, {
                reload: true, inherit: false, notify: true
            });
        }
        /*详情*/
        $scope.MoveVideo = function(){
            $(".detaiTab").addClass("detailsa");
            $(".cataTab").removeClass("detailsa");
            $(".videoTab").removeClass("detailsa");
            $(document).scrollTop($("#tabBox").offset().top);
        }
        /*点击跳转到视频目录*/
        $scope.MovevideoCata = function(){

            $(".detaiTab").removeClass("detailsa");
            $(".cataTab").addClass("detailsa");
            $(".videoTab").removeClass("detailsa");
            $(document).scrollTop($("#catalog").offset().top - 48);

        };
        /*点击跳转到评价*/
        $scope.MoveEvaluCon=function(){

            $(".detaiTab").removeClass("detailsa");
            $(".cataTab").removeClass("detailsa");
            $(".videoTab").addClass("detailsa");
            $(document).scrollTop($("#evaluateCon").offset().top - 48);

        }
        $scope.companyout=function(){
            $state.go("outIndexCtrl",{companyId:$scope.compangyInfo.userId})
        }
        $scope.companyout2=function(){
            $state.go("othersMainPage",{userId:$scope.teachId})
        }
        /*相关视频接口*/
        function SameVideo(){
             var req ={
                 type:"GET",
                 uri:"getSameCourse",
                 data:{
                 	courseId:$stateParams.courseId
                 }
             };
             httpsevices.getcourse(req).success(function(response){
                 $scope.$apply(function(){
                     $scope.SameVideoCon = response.data.list;
                 })
             });
        }
        SameVideo();

        $scope.commentConf = {
            showStar: true,
            tbWidth:745,
            conf: {
                id: $stateParams.courseId,
                type: 8
            }
        };

        $scope.isAddCar = false;
        $scope.addCar = function(){
            auth.isLogin().then(function(){

            });
            var req = {
                type:'POST',
                uri:'add',
                data:{
                    courseId:$scope.TopLink.course.courseId,
                    money:$scope.TopLink.course.price,
                    number:1
                }
            };
            httpsevices.shoppingHttpService(req).success(function(response){
                $scope.$apply(function(){
                    $scope.isAddCar = true;
                })
            }).error(function(response){

            })
        };
        $scope.closeCar = function(){
            $scope.isAddCar = false;
        }




    })
})